// pages/authentication/authentication.js
var common = require("../../utils/common.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: '',
    beginTime: 60,
    maskTitle: '获取验证码',
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isAuto){
      wx.navigateBack()
    }
  },

  queryHasAuth: function () {
    var that = this;
    common.ajaxPost(
      'portal/wlpUsers/queryHasAuth.action', {
        xcxOpenId: app.globalData.xcxOpenId,
        bid: app.globalData.bid
      },
      function (res) {
        if (res && res["result"]) { 
            // setTimeout(function(){
            //   wx.navigateBack()
            // },1500)
        }
      }
    )
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.isGoBack = true;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  currentTime: function () {
    //倒计时
    var that = this;
    var beginTime = that.data.beginTime;
    var times = setTimeout(function () {
      console.log(beginTime);
      app.globalData.sendVerificationCode = false;
      if (beginTime == '0') {
        that.setData({
          maskTitle: '再次获取',
          beginTime: 60
        })
        app.globalData.sendVerificationCode = true;
        return false;
      }
      else {
        beginTime--;
        that.setData({
          beginTime: beginTime,
          maskTitle: beginTime + '秒'
        })
        that.currentTime();
      }
    }, 1000);
  },

  getYzm: function () {
    //获取验证码
    var inputValue = (this.data.inputValue);
    var that = this;
    console.log(inputValue);
    if (inputValue == "") {
      wx.showToast({
        title: '请输入您的手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (inputValue.length < 11) {
      wx.showToast({
        title: '请检查您的手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (!common.checkphone(inputValue)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
    } else {
      //发送验证码请求
      //倒计时
      //获取手机号
      if (app.globalData.sendVerificationCode == true) {
        var tel = that.data.inputValue;
        common.ajaxPost(
          'portal/userInfo/sendVerificationCode.action', {
            bid: app.globalData.bid,
            checkTel: 'F',
            tel: tel
          },
          function (res) {
            if (res && res["result"]) {
              that.currentTime();
              wx.showToast({
                title: '短信发送成功',
                icon: 'none',
                duration: 2000
              })
            }
          })
        //  
      } else {
        return false;
      }
    }
  },

  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  formSubmit: function (e) {
    var formInput = e.detail.value;
    var that=this;
    console.log()
    if (formInput.name == "") {
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
        duration: 2000
      })
    } else if (formInput.phone == "") {
      wx.showToast({
        title: '请输入您的手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (formInput.phone.length < 11) {
      wx.showToast({
        title: '请检查您的手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (!common.checkphone(formInput.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (formInput.code == "") {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
    } else {
      //提交数据
      console.log(formInput);
      common.ajaxPost(
        'portal/wlpUsers/auth.action', {
          xcxOpenId: app.globalData.xcxOpenId,
          bid: app.globalData.bid,
          name: formInput.name,
          phone: formInput.phone,
          verCode: formInput.code
        },
        function (res) {
          if (res && res["result"]) {
            that.setData({
              resInfo:res
            })
            wx.showToast({
              title: res.resultMsg,
              icon: 'none',
              duration: 2000
            });
            setTimeout(function(){
                 that.goBack()
            },2500)
          }
        })
     }
  },
  goBack:function(){
    //认证成功获取，用户信息
    var that=this;
    var GwHeadImage = that.data.resInfo.GwHeadImage;
    var GwName = that.data.resInfo.GwName;
    var GwUserType = that.data.resInfo.GwUserType;
    var Gwid = that.data.resInfo.Gwid;
    var bid = app.globalData.bid;
    
    wx.navigateTo({
      url: '../tmList/tmList?GwHeadImage=' + GwHeadImage + "&GwName=" + GwName + "&GwUserType=" + GwUserType + "&Gwid=" + Gwid + '&bid=' + bid,
    })
  }
})